package com.community.sqlapp.dpsToXmind;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class QwenHttpClientUtil {
    @Value("${qianwenvl.vlapi.vlkey}")
    private String dashscopeApiKey;
    @Value("${qianwenvl.vlapi.vlurl}")
    private String dashscopeApiUrl;
    @Value("${qianwenvl.vlapi.qwenlongurl}")
    private String dashscopeqwenlongurl;
    /**
     * 千问vl大模型
     * @param jsonBody
     * @return
     * @throws IOException
     */
    public String sendPostRequest(String jsonBody) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(dashscopeApiUrl);

        // 设置请求头
        httpPost.setHeader("Authorization", "Bearer " + dashscopeApiKey);
        httpPost.setHeader("Content-Type", "application/json");

        // 设置请求体
        httpPost.setEntity(new StringEntity(jsonBody, "UTF-8"));

        try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
            return EntityUtils.toString(response.getEntity());
        } finally {
            httpClient.close();
        }
    }

    /**
     * 千问long大模型
     * @param jsonBody
     * @return
     * @throws IOException
     */
    public String sendGetLongRequest(String jsonBody) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(dashscopeqwenlongurl);

        // 设置请求头
        httpPost.setHeader("Authorization", "Bearer " + dashscopeApiKey);
        httpPost.setHeader("Content-Type", "application/json");

        // 设置请求体
        httpPost.setEntity(new StringEntity(jsonBody, "UTF-8"));

        try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
            return EntityUtils.toString(response.getEntity());
        } finally {
            httpClient.close();
        }
    }
}